/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bean;

import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import utilitaire.GestionSQL;

/**
 *
 * @author romain_lou
 */
public class Parcelle {

    private ArrayList<Coordonne> listeCoordonnee;
    private String commune, culture;
    private Double surface;

    public Parcelle(){
        this.listeCoordonnee = new ArrayList<Coordonne>();
    }

    public String getCommune() {
        return commune;
    }

    public void setCommune(String commune) {
        this.commune = commune;
    }

    public ArrayList<Coordonne> getListeCoordonnee() {
        return listeCoordonnee;
    }

    public void setListeCoordonnee(ArrayList<Coordonne> listeCoordonnee) {
        this.listeCoordonnee = listeCoordonnee;
    }

    public Double getSurface() {
        return getHa(surface)/100.0;
    }

    public void setSurface(Double surface) {
        this.surface = surface;
    }

    private Double getHa(Double surface){
        return surface;
    }

    public String getCulture() {
        if(this.culture.equals("CH"))
            return "COLZA";
        if(this.culture.equals("BH"))
            return "BLE";
        return "";
    }

    public void setCulture(String culture) {
        this.culture = culture;
    }

    public int save(Ilot _ilot){

        int idParcelle = idParcelle(_ilot);

        if(idParcelle==0 && _ilot.getPersonne().getId() != 0){
            //save Parcelle
            String requete = "INSERT INTO  nomparcelle (id_client "
                + ",ilot ,superficie ,precedent ,variete)VALUES (";
            requete += "'"+_ilot.getPersonne().getId()+"', ";
            requete += "'"+_ilot.getNumIlot()+"', ";
            requete += "'"+getSurface()+"', ";
            requete += "'"+getCulture()+"', ";
            requete += "'B' )";
            
            utilitaire.Log.getLog().ecrire("Creation parcelle "+_ilot.getNumIlot());

            Statement statement;
            try {
                statement = (Statement) GestionSQL.getConnection().getCon().createStatement();

            statement.executeUpdate(requete);

            idParcelle = idParcelle(_ilot);

            } catch (ClassNotFoundException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            } catch (InstantiationException ex) {
               utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            } catch (IllegalAccessException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            } catch (SQLException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            }

            }

       return idParcelle;
    }

        private int idParcelle(Ilot _ilot){
            Statement statement = null;
                int id = 0;
            try {
                statement = (Statement) GestionSQL.getConnection().getCon().createStatement();
            } catch (ClassNotFoundException ex) {
               utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            } catch (InstantiationException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            } catch (IllegalAccessException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            } catch (SQLException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            }
            // Result set get the result of the SQL query

            ResultSet resultSet = null;
            try {
                resultSet = statement.executeQuery("select * from nomparcelle where id_client = '"+_ilot.getPersonne().getId()+"' "
                    + "AND ilot = '"+_ilot.getNumIlot()+"' AND superficie = '"+getSurface()+"' AND precedent = '"+getCulture()+"'");
                while (resultSet.next()) {
                    id = resultSet.getInt("id_nomParcelle");
                }


            } catch (SQLException ex) {
                utilitaire.Log.getLog().ecrireErreur(ex.getMessage());
            }

            return id;
        }

    
}
